<?php
namespace App\Http\Controllers\Admin;
use App\Http\Controllers\Controller;
use App\Services\OSS;
use Illuminate\Http\Request;
use App\Services\TeachingAidsService;
use App\Services\TeachingAidService;
use Session;

class TeachingAidsController extends Controller{

	public function uploadTeachAid(Request $request){
		$file = $request->file('material_file');

		if(!$file->isValid()){
			$data = array('code'=>400);
		}else{
			// 获取目录名称
			$dir_name = $request->input('teaching_aids');
			if($dir_name == ''){
				$dir_name = 'teach_aid';
			}			
			// 获取扩展名
			$ext = $file->getClientOriginalExtension();

			// 生成新文件名
			$newfile = md5(time().rand(1000,9999)).'.'.$ext;

			// 文件上传指定路径
			$path = public_path($dir_name);

			$target_file = $dir_name.'/'.$newfile;

			$res = OSS::upload($target_file,$file->getRealPath());

			if($res){
				$data = array('code'=>200,'imgurl' => $target_file);
			}else{
				$data = array('code'=>400);
			}

		}

		return response()->json($data);
	}

	// 教具包列表
	public function talist(Request $request){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$taids = new TeachingAidsService;

			$data['username'] = $username;
			$data['page_size_list'] = array(20,25,50,100);
			$data['page_size'] = $request->input('page_size') ? intval($request->input('page_size')) : 3;
			$data['keywords'] = $request->input('keywords') ? addslashes($request->input('keywords')) : '';
			$data['aidslist'] = $taids->getTaList($data['page_size'],$data['keywords']);

			return view('admin.teach_aids.ta_list',$data);
		}
	}

	// 添加教具包
	public function createTa(){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			return view('admin.teach_aids.ta_add',$data);
		}
	}

	// 插入教具包
	public function insertTa(){
		date_default_timezone_set('PRC');

		$taids = new TeachingAidsService;

		$data['name'] = addslashes($_POST['name']);
		$data['num'] = intval($_POST['num']);
		$data['stock'] = intval($_POST['stock']);
		$data['price'] = addslashes($_POST['price']);
		$data['aid_num'] = intval($_POST['aid_num']);
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;
		$data['content'] = addslashes($_POST['content']);
		$aidsInfo = $taids->getTaInfoByName($data['name']);
		if(!empty($aidsInfo)){
			$_data = array('code'=>300);
		}else{
			$res = $taids->insertTa($data);
			if($res){
				$_data = array('code'=>200);
			}else{
				$_data = array('code'=>400);
			}
		}

		return response()->json($_data);

	}

	// 编辑教具包
	public function editTa($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$taids = new TeachingAidsService;
			$data['username'] = $username;
			$data['aidinfo'] = $taids->getTaInfoById($id);
			return view('admin.teach_aids.ta_edit',$data);
		}
	}

	// 修改教具包
	public function updateTa(){
		$taids = new TeachingAidsService;
		$id = intval($_POST['id']);

		$data['name'] = addslashes($_POST['name']);
		$data['num'] = intval($_POST['num']);
		$data['stock'] = intval($_POST['stock']);
		$data['price'] = addslashes($_POST['price']);
		$data['aid_num'] = intval($_POST['aid_num']);
		$data['update_date'] = date('Y-m-d H:i:s');
		$data['content'] = addslashes($_POST['content']);
		$res = $taids->modifyTa($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);

	}

	// 删除教具包
	public function delTa($id){
		$taids = new TeachingAidsService;
		$data['isshow'] = 0;
		$data['update_date'] = date('Y-m-d H:i:s');
		$res = $taids->modifyTa($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);
	}

	// 查看教具包
	public function viewTa($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$taid = new TeachingAidService;

			$data['username'] = $username;
			$data['aidList'] = $taid->getAidList($id);
			$data['id'] = $id;
		
			return view('admin.teach_aids.aid_list',$data);
		}
	}

	// 添加教具
	public function create($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$data['username'] = $username;
			$data['id'] = $id;
			return view('admin.teach_aids.aid_add',$data);
		}
	}

	// 插入教具

	public function insert(){
		date_default_timezone_set('PRC');
		$taid = new TeachingAidService;

		$data['at_id'] = intval($_POST['tid']);
		$data['picture'] = addslashes($_POST['picture']);
		$data['name'] = addslashes($_POST['name']);
		$data['desc'] = addslashes($_POST['desc']);
		$data['price'] = addslashes($_POST['price']);
		$data['mprice'] = addslashes($_POST['mprice']);
		$data['item_num'] = 'NO-'.mt_rand(100000,999999);
		$data['status'] = 1;
		$data['stock'] = intval($_POST['stock']);
		$data['unit'] = addslashes($_POST['unit']);
		$data['add_date'] = date('Y-m-d H:i:s');
		$data['isshow'] = 1;

		$taidInfo = $taid->getAidInfoByName($data);
		if(!empty($taidInfo)){
			$_data = array('code'=>300);
		}else{
			$res = $taid->insertAid($data);
			if($res){
				$_data = array('code'=>200);
			}else{
				$_data = array('code'=>400);
			}
		}
		return response()->json($_data);
	}

	// 修改教具
	public function edit($id){
		$username = Session::get('username');
		if(!$username){
			return redirect('/admin/login');
		}else{
			$taid = new TeachingAidService;
			$data['username'] = $username;
			$data['aidInfo'] = $taid->getAidInfoById($id);
			return view('admin.teach_aids.aid_edit',$data);
		}
	}

	// 保存教具
	public function update(){
		date_default_timezone_set('PRC');
		$taid = new TeachingAidService;

		$id = intval($_POST['id']);
		$data['at_id'] = intval($_POST['tid']);
		$data['picture'] = addslashes($_POST['picture']);
		$data['name'] = addslashes($_POST['name']);
		$data['desc'] = addslashes($_POST['desc']);
		$data['price'] = addslashes($_POST['price']);
		$data['mprice'] = addslashes($_POST['mprice']);
		$data['item_num'] = 'NO-'.mt_rand(100000,999999);
		$data['status'] = 1;
		$data['stock'] = intval($_POST['stock']);
		$data['unit'] = addslashes($_POST['unit']);
		$data['update_date'] = date('Y-m-d H:i:s');

		$res = $taid->modifyAid($id,$data);

		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}

		return response()->json($_data);

	}

	// 删除教具
	public function del($id){
		$taid = new TeachingAidService;

		$data['isshow'] = 0;
		$data['update_date'] = date('Y-m-d H:i:s');
		$res = $taid->modifyAid($id,$data);
		if($res){
			$_data = array('code'=>200);
		}else{
			$_data = array('code'=>400);
		}
		return response()->json($_data);
	}
}
